﻿using System.Collections;
using System.Collections.Generic;
using System;
using System.Text;
using DAL;
using BLL;
using System.IO;
using System.Data;
using System.Xml;
using System.Web.Security;
using System.Security.Cryptography;
using System.Net;

namespace Web.Old_App_Code
{
    public class AlipayConfig
    {
        // Fields
        private string input_charset;
        private string key;
        private string mainname;
        private string notify_url;
        private string partner;
        private string return_url;
        private string seller_email;
        private string show_url;
        private string sign_type;
        private string transport;

        // Methods
        public AlipayConfig() { }

        // Properties
        public string Input_charset
        {
            get
            {
                return this.input_charset;
            }
            set
            {
                this.input_charset = value;
            }
        }


        public string Key
        {
            get
            {
                return this.key;
            }
            set
            {
                this.key = value;
            }
        }




        public string Mainname
        {
            get
            {
                return this.mainname;
            }
            set
            {
                this.mainname = value;
            }
        }


        public string Notify_url
        {
            get
            {
                return this.notify_url;
            }
            set
            {
                this.notify_url = value;
            }
        }


        public string Partner
        {
            get
            {
                return this.partner;
            }
            set
            {
                this.partner = value;
            }
        }




        public string Return_url
        {
            get
            {
                return this.return_url;
            }
            set
            {
                this.return_url = value;
            }
        }


        public string Seller_email
        {
            get
            {
                return this.seller_email;
            }
            set
            {
                this.seller_email = value;
            }
        }


        public string Show_url
        {
            get
            {
                return this.show_url;
            }
            set
            {
                this.show_url = value;
            }
        }




        public string Sign_type
        {
            get
            {
                return this.sign_type;
            }
            set
            {
                this.sign_type = value;
            }
        }




        public string Transport
        {
            get
            {
                return this.transport;
            }
            set
            {
                this.transport = value;
            }
        }

    }

    public class AlipayFunction
    {
        // Methods
        public static string Build_mysign(Dictionary<string, string> dicArray, string key, string sign_type, string _input_charset)
        {
            string str = Create_linkstring(dicArray);
            int length = str.Length;
            return Sign(str.Substring(0, length - 1) + key, sign_type, _input_charset);
        }

        public static string Create_linkstring(Dictionary<string, string> dicArray)
        {
            StringBuilder builder = new StringBuilder();
            foreach (KeyValuePair<string, string> pair in dicArray)
            {
                builder.Append(pair.Key + "=" + pair.Value + "&");
            }
            return builder.ToString();
        }

        public static void log_result(string sPath, string sWord)
        {
            StreamWriter writer = new StreamWriter(sPath, false, Encoding.Default);
            writer.Write(sWord);
            writer.Close();
        }

        public static Dictionary<string, string> Para_filter(SortedDictionary<string, string> dicArrayPre)
        {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            foreach (KeyValuePair<string, string> pair in dicArrayPre)
            {
                if (((pair.Key.ToLower() != "sign") && (pair.Key.ToLower() != "sign_type")) && (pair.Value != ""))
                {
                    dictionary.Add(pair.Key.ToLower(), pair.Value);
                }
            }
            return dictionary;
        }

        public static string Query_timestamp(string partner)
        {
            string url = "https://mapi.alipay.com/gateway.do?service=query_timestamp&partner=" + partner;
            XmlTextReader reader = new XmlTextReader(url);
            XmlDocument document = new XmlDocument();
            document.Load(reader);
            return document.SelectSingleNode("/alipay/response/timestamp/encrypt_key").InnerText;
        }

        public static string Sign(string prestr, string sign_type, string _input_charset)
        {
            StringBuilder builder = new StringBuilder(0x20);
            if (sign_type.ToUpper() == "MD5")
            {
                byte[] buffer = new MD5CryptoServiceProvider().ComputeHash(Encoding.GetEncoding(_input_charset).GetBytes(prestr));
                for (int i = 0; i < buffer.Length; i++)
                {
                    builder.Append(buffer[i].ToString("x").PadLeft(2, '0'));
                }
            }
            return builder.ToString();
        }
    }




    public class AlipayNotify
    {
        // Fields
        private string _input_charset = "";
        private string _key = "";
        private string _partner = "";
        private string _sign_type = "";
        private string _transport = "";
        private string gateway = "";
        private string mysign = "";
        private string preSignStr = "";
        private string responseTxt = "";
        private Dictionary<string, string> sPara = new Dictionary<string, string>();

        // Methods
        public AlipayNotify(SortedDictionary<string, string> inputPara, string notify_id, string partner, string key, string input_charset, string sign_type, string transport)
        {
            this._transport = transport;
            if (this._transport == "https")
            {
                this.gateway = "https://www.alipay.com/cooperate/gateway.do?";
            }
            else
            {
                this.gateway = "http://notify.alipay.com/trade/notify_query.do?";
            }
            this._partner = partner.Trim();
            this._key = key.Trim();
            this._input_charset = input_charset;
            this._sign_type = sign_type.ToUpper();
            this.sPara = AlipayFunction.Para_filter(inputPara);
            this.preSignStr = AlipayFunction.Create_linkstring(this.sPara);
            this.mysign = AlipayFunction.Build_mysign(this.sPara, this._key, this._sign_type, this._input_charset);
            this.responseTxt = this.Verify(notify_id);
        }

        private string Get_Http(string strUrl, int timeout)
        {
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(strUrl);
                request.Timeout = timeout;
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.Default);
                StringBuilder builder = new StringBuilder();
                while (-1 != reader.Peek())
                {
                    builder.Append(reader.ReadLine());
                }
                return builder.ToString();
            }
            catch (Exception exception)
            {
                return ("错误：" + exception.Message);
            }
        }

        private string Verify(string notify_id)
        {
            string strUrl = "";
            if (this._transport == "https")
            {
                strUrl = this.gateway + "service=notify_verify&partner=" + this._partner + "&notify_id=" + notify_id;
            }
            else
            {
                strUrl = this.gateway + "partner=" + this._partner + "&notify_id=" + notify_id;
            }
            return this.Get_Http(strUrl, 0x1d4c0);
        }

        // Properties
        public string Mysign
        {
            get
            {
                return this.mysign;
            }
        }

        public string PreSignStr
        {
            get
            {
                return this.preSignStr;
            }
        }

        public string ResponseTxt
        {
            get
            {
                return this.responseTxt;
            }
        }
    }



    public class AlipayService
    {
        // Fields
        private string _input_charset = "";
        private string _key = "";
        private string _sign_type = "";
        private string gateway = "";
        private string mysign = "";
        private Dictionary<string, string> sPara = new Dictionary<string, string>();

        // Methods
        public AlipayService(string partner, string seller_email, string return_url, string notify_url, string show_url, string out_trade_no, string subject, string body, string total_fee, string paymethod, string defaultbank, string anti_phishing_key, string exter_invoke_ip, string extra_common_param, string buyer_email, string royalty_type, string royalty_parameters, string key, string input_charset, string sign_type)
        {
            this.gateway = "https://www.alipay.com/cooperate/gateway.do?";
            this._key = key.Trim();
            this._input_charset = input_charset.ToLower();
            this._sign_type = sign_type.ToUpper();
            SortedDictionary<string, string> dicArrayPre = new SortedDictionary<string, string>();
            dicArrayPre.Add("service", "create_direct_pay_by_user");
            dicArrayPre.Add("payment_type", "1");
            dicArrayPre.Add("partner", partner);
            dicArrayPre.Add("seller_email", seller_email);
            dicArrayPre.Add("return_url", return_url);
            dicArrayPre.Add("notify_url", notify_url);
            dicArrayPre.Add("_input_charset", this._input_charset);
            dicArrayPre.Add("show_url", show_url);
            dicArrayPre.Add("out_trade_no", out_trade_no);
            dicArrayPre.Add("subject", subject);
            dicArrayPre.Add("body", body);
            dicArrayPre.Add("total_fee", total_fee);
            dicArrayPre.Add("paymethod", paymethod);
            dicArrayPre.Add("defaultbank", defaultbank);
            dicArrayPre.Add("anti_phishing_key", anti_phishing_key);
            dicArrayPre.Add("exter_invoke_ip", exter_invoke_ip);
            dicArrayPre.Add("extra_common_param", extra_common_param);
            dicArrayPre.Add("buyer_email", buyer_email);
            dicArrayPre.Add("royalty_type", royalty_type);
            dicArrayPre.Add("royalty_parameters", royalty_parameters);
            this.sPara = AlipayFunction.Para_filter(dicArrayPre);
            this.mysign = AlipayFunction.Build_mysign(this.sPara, this._key, this._sign_type, this._input_charset);
        }

        public string Build_Form()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("<form id=\"alipaysubmit\" name=\"alipaysubmit\" action=\"" + this.gateway + "_input_charset=" + this._input_charset + "\" method=\"get\">");
            foreach (KeyValuePair<string, string> pair in this.sPara)
            {
                builder.Append("<input type=\"hidden\" name=\"" + pair.Key + "\" value=\"" + pair.Value + "\"/>");
            }
            builder.Append("<input type=\"hidden\" name=\"sign\" value=\"" + this.mysign + "\"/>");
            builder.Append("<input type=\"hidden\" name=\"sign_type\" value=\"" + this._sign_type + "\"/>");
            builder.Append("<input type=\"submit\" value=\"支付宝确认付款\"></form>");
            builder.Append("<script>document.forms['alipaysubmit'].submit();</script>");
            return builder.ToString();
        }
    }


}

 
 
